package com.ibm.sc.osp.sample.service.impl;

import com.ibm.sc.osp.base.core.util.StringUtils;
import com.ibm.sc.osp.base.core.util.pagination.PageAdapter;
import com.ibm.sc.osp.sample.domain.entity.PfModel;
import com.ibm.sc.osp.sample.repository.PfModelRepository;
import com.ibm.sc.osp.sample.service.PfModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings("all")
public class PfModelServiceImpl implements PfModelService {
    @Autowired
    private PfModelRepository repository;
    
    @Override
    public PfModel savePfModel(PfModel model) {
        return repository.saveAndFlush(model);
    }
    
    @Override
    public PfModel getPfModel(String id) {
        return repository.getOne(id);
    }
    
    @Override
    public boolean checkPfModel(String name, String method) {
        return repository.existsDistinctByTemplateNameAndSendMethod(name, method);
    }
    
    @Override
    public PageAdapter findPfModelByPage(PageAdapter<PfModel> pageAdapter, PfModel model) {
        Page<PfModel> list;
        int count = 0;
        if (!StringUtils.isBlank(model.getTemplateName())) {
            count += 1;
        }
        if (!StringUtils.isBlank(model.getTemplatType())) {
            count += 2;
        }
        if (!StringUtils.isBlank(model.getSendMethod())) {
            count += 4;
        }
        switch (count) {
            case 1:
                list = (Page<PfModel>) repository.findAllByTemplateName(model.getTemplateName(), pageAdapter);
                break;
            case 2:
                list = (Page<PfModel>) repository.findAllByTemplatType(model.getTemplatType(), pageAdapter);
                break;
            case 3:
                list = (Page<PfModel>) repository.findAllByTemplateNameAndTemplatType(model.getTemplateName(), model.getTemplatType(), pageAdapter);
                break;
            case 4:
                list = (Page<PfModel>) repository.findAllBySendMethod(model.getSendMethod(), pageAdapter);
                break;
            case 5:
                list = (Page<PfModel>) repository.findAllByTemplateNameAndSendMethod(model.getTemplateName(), model.getSendMethod(), pageAdapter);
                break;
            case 6:
                list = (Page<PfModel>) repository.findAllByTemplatTypeAndSendMethod(model.getTemplatType(), model.getSendMethod(), pageAdapter);
                break;
            case 7:
                list = (Page<PfModel>) repository.findAllByTemplateNameAndTemplatTypeAndSendMethod(model.getTemplateName(), model.getTemplatType(), model.getSendMethod(), pageAdapter);
                break;
            default:
                list = (Page<PfModel>) repository.findAll(pageAdapter);
                break;
        }
        return new PageAdapter<>(list);
    }
    
    @Override
    public boolean deleteModel(String id) {
        if (repository.exists(id)) {
            repository.delete(id);
            return true;
        }
        return false;
    }
}
